/**
 * 实体
 */
Ext.namespace('Divo');
Ext.namespace('Divo.app');
Divo.app.CompanySelGridWin = Ext.extend(Ext.Window, {
  OrgGrid : null,
  queryForm : null,
  queryTextField : null,
  query : null,
  viewSel : null,// 过滤视图
  viewType : null,
  initComponent : function() {

    this.OrgGrid = this.getGrid();
    this.queryTextField = this.getField();
    this.queryForm = this.getForm();
    Ext.apply(this, {
          title : "选择高值耗材",
          iconCls : 'icon-win',
          autoHeight : false,
          height : 450,
          width : 600,
          layout : 'border',
          plain : false,
          buttonAlign : 'right',
          closable : false,
          draggable : false,
          modal : true,
          items : [{
                border : false,
                layout : 'fit',
                region : 'north',
                height : 100,
                items : [this.queryForm]
              }, {
                border : false,
                layout : 'fit',
                region : 'center',
                items : [this.OrgGrid]
              }],
          buttons : [{
                text : '确定',
                handler : this.selOk.createDelegate(this)
              }, {
                text : '关闭',
                handler : this.onWinHide.createDelegate(this)
              }]
        });

    Divo.app.CompanySelGridWin.superclass.initComponent.apply(this, arguments);
    this.addEvents('exportSelectItem');
  },

  render : function() {
    Divo.app.CompanySelGridWin.superclass.render.apply(this, arguments);
  },

  initEvents : function() {
    Divo.app.CompanySelGridWin.superclass.initEvents.call(this);
    this.on("show", this.onWinShow, this);
  },

  showDialog : function() {
    this.show();
  },

  selOk : function() {
    var record = this.OrgGrid.getSelectionModel().getSelected();
    if (record) {
      this.fireEvent("exportSelectItem", record.data.id, record.data.materialName,
          record.data.name);
      this.hide();
    } else {
      Ext.Msg.alert("提示", "你还没有选取高值耗材");
    }
  },

  onQuery : function() {
    this.query = this.queryTextField.getValue();

    this.OrgGrid.getStore().load();
  },

  onWinShow : function() {
    if (this.OrgGrid) {
      this.OrgGrid.getStore().load();
    }
    this.queryTextField.setValue("");
  },

  onWinHide : function() {
    this.hide();
  },

  getField : function() {
    if (this.queryTextField) {
      return this.queryTextField;
    } else {
      var me = this;
      var textfield = new Ext.form.TextField({
            name : 'queryName',
            fieldLabel : '输入名称、拼音、五笔回车查询',
            allowBlank : true,
            width : 200,
            listeners : {
              "specialkey" : function(o, e) {
                if (e.getKey() === e.ENTER) {
                  me.onQuery();
                }
              }
            }
          });
      return textfield;
    }
  },

  getForm : function() {
    if (this.queryForm) {
      return this.queryForm;
    } else {

      var me = this;

      this.viewSel = new app.base.ComboBox({
            name : "viewSelect",
            dataIndex : "viewSelect",
            fieldLabel : '耗材类型',
            store : Divo.com.Lib.getHighValueType(),
            displayField : 'name',
            valueField : 'id',
            allowBlank : false,
            forceSelection : true,
            mode : 'remote',
            triggerAction : 'all',
            editable : false,
            selectOnFocus : true,
            onSelect : function(record) {
              var data = record.data;
              this.setValue(data.name);
              me.viewType = data.id;

              me.OrgGrid.getStore().reload();
              this.onTriggerClick();
            }
          });

      var queryPanel = new Ext.form.FormPanel({
            style : 'padding:0px 0px 0px 0px',
            height : "100%",
            frame : true,
            labelWidth : 100,
            labelAlign : "right",
            layout : "form",
            name : "QUERYPANEL",
            width : "100%",
            items : [this.viewSel, this.queryTextField]
          });
      return queryPanel;
    }
  },

  getGrid : function() {

    if (this.OrgGrid) {
      return this.OrgGrid;
    } else {

      var grid = new Divo.Base.GridView({
        gridId : "query-company",
        // queryFlds : queryFlds,
        // searchFlds : searchFlds,
        entityClassName : 'com.our.sys.project1.entity.HighValueMaterialDetail',
        queryUrl : 'high_value_material/query_d',
        jsonId : 'id',
        recordPk : ["id"]
      });

      grid.getStore().on('beforeload', function(store, options) {
            store.baseParams.query = this.query ? this.query : '';
            store.baseParams.sId = this.viewType;
          }, this, true);

      grid.on('dblclick', this.selOk.createDelegate(this), this);
      return grid;
    }
  }
});

Ext.reg('Divo.app.CompanySelGridWin', Divo.app.CompanySelGridWin);
